package com.hero.take.util;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class CommonConvert {

    /**
     * 数据转化
     *
     * @param sources
     * @param function
     * @param <T>
     * @param <R>
     * @return
     */
    public static <T, R> List<R> convert(List<T> sources, Function<T, R> function) {
        List<R> result = new ArrayList<>();
        if (!ListHelpper.isValid(sources)) return result;
        sources.forEach(source -> {
            R r = function.apply(source);
            if (r != null)
                result.add(r);
        });
        return result;
    }

    /**
     * 数据转化
     *
     * @param sources
     * @param function
     * @param <T>
     * @param <R>
     * @return
     */
    public static <T, R> List<R> convert(T[] sources, Function<T, R> function) {
        List<R> result = new ArrayList<>();
        if (sources == null || sources.length == 0) return result;
        for (T source : sources) {
            R r = function.apply(source);
            if (r != null)
                result.add(r);
        }
        return result;
    }
}
